package com.dormitory.activity;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.dormitory.R;
import com.dormitory.adapter.MemberListAdapter;
import com.dormitory.bean.UserBean;
import com.dormitory.controller.UserController;

import java.util.List;

public class MemberActivity extends AppCompatActivity {

    ListView mMemberList;
    MemberListAdapter mMemberListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member);


        mMemberList = (ListView)findViewById(R.id.member_list);
        mMemberListAdapter = new MemberListAdapter(this);
        mMemberList.setAdapter(mMemberListAdapter);

        int id = getIntent().getIntExtra("dormitoryId", -1);
        if(id != -1) {
            GetMemberAsyncTask getMemberAsyncTask = new GetMemberAsyncTask();
            getMemberAsyncTask.execute(id);
        }
    }
    private class GetMemberAsyncTask extends AsyncTask<Integer, Object, List<UserBean>> {

        @Override
        protected List<UserBean> doInBackground(Integer[] params) {
            UserController userController = UserController.getInstance();
            try {
                return userController.getMember(params[0]);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(List<UserBean> list) {
            super.onPostExecute(list);
            if(list == null) {
                Toast.makeText(MemberActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
            else
                mMemberListAdapter.updateUser(list);
        }
    }
}
